<!-- 现存量查询 -->
<template>
	<view class="content">
		<z-paging ref="paging" v-model="dataList" @query="queryList" :refresher-enabled="false">
			<template slot="top">
				<!-- 顶部导航栏 -->
				<u-navbar :fixed="false" :title="titleName" :autoBack="true" :safeAreaInsetTop="true"
					leftIconColor="#fff">
					<template slot="right">
						<uni-icons type="search" size="24" color="#fff" @click="showSearch=true"></uni-icons>
					</template>
				</u-navbar>
			</template>

			<view class="content-refresh">
				<view class="module" v-for="(row,index) in dataList">
					<view>
						<u-row>
							<u-col span="6">
								<view class="itemContent">
									<view class="itemContent_label">
										<text>仓库名称</text>
									</view>
									<view class="itemContent_value">
										<text>{{row.FWarehouseName}}</text>
									</view>
								</view>
							</u-col>
							<u-col span="6">
								<view class="itemContent">
									<view class="itemContent_label">
										<text>库位编码</text>
									</view>
									<view class="itemContent_value">
										<text>{{row.FStorageCode}}</text>
									</view>
								</view>
							</u-col>
						</u-row>
						<u-row>
							<u-col span="12">
								<view class="itemContent">
									<view class="itemContent_label">
										<text>产品编码</text>
									</view>
									<view class="itemContent_value">
										<text>{{row.ProductCode}}</text>
									</view>
								</view>
							</u-col>
						</u-row>
						<u-row>
							<u-col span="12">
								<view class="itemContent">
									<view class="itemContent_label">
										<text>产品名称</text>
									</view>
									<view class="itemContent_value">
										<text>{{row.ProductName}}</text>
									</view>
								</view>
							</u-col>
						</u-row>
						<u-row>
							<u-col span="6">
								<view class="itemContent">
									<view class="itemContent_label">
										<text>产品批次</text>
									</view>
									<view class="itemContent_value">
										<text>{{row.FBatchNo}}</text>
									</view>
								</view>
							</u-col>
							<u-col span="6">
								<view class="itemContent">
									<view class="itemContent_label">
										<text>当前库存</text>
									</view>
									<view class="itemContent_value">
										<text>{{row.Qty}}</text>
									</view>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
			</view>

			<!-- 底部合计 -->
			<template #bottom>
				<view class="total" :style="$utils.disposeSafeBottomFun()">
					<text class="total_label">
						库存总数
					</text>
					<text class="total_value">
						{{totalQty}}
					</text>
				</view>
			</template>
		</z-paging>

		<!-- 新增按钮 -->
		<view class="scanButton" v-if="!$utils.isPda()">
			<uni-icons type="scan" size="36" color="#51D0D0" @click="openScan"></uni-icons>
		</view>

		<!-- 搜索弹窗 -->
		<u-popup :show="showSearch" :safeAreaInsetBottom="true" @close="closeSearch">
			<view :style="$utils.disposeSafeBottomFun()" class="listSearch">
				<u--form labelPosition="left" :model="searchData" labelWidth="100" ref="uForm">
					<u-form-item label="仓库名称" prop="FWarehouseName" borderBottom>
						<view @click="openBaseScreen('仓库')" style="flex:1;">
							<u--input :value="searchData.FWarehouseName" readonly placeholder="请选择仓库"
								border="none"></u--input>
						</view>
						<view slot="right" v-if="searchData.FWarehouseID">
							<u-icon name="close" @click="clearCondition(1)"></u-icon>
						</view>
					</u-form-item>
					<u-form-item label="库位编码" prop="FStorageCode" borderBottom>
						<view @click="openBaseScreen('库位')" style="flex:1;">
							<u--input :value="searchData.FStorageCode" readonly placeholder="请选择库位"
								border="none"></u--input>
						</view>
						<view slot="right" v-if="searchData.FStorageID">
							<u-icon name="close" @click="clearCondition(2)"></u-icon>
						</view>
					</u-form-item>
					<u-form-item label="产品编码" prop="ProductCode" borderBottom>
						<view @click="openBaseScreen('产品')" style="flex:1;">
							<u--input :value="searchData.ProductCode" readonly placeholder="请选择产品"
								border="none"></u--input>
						</view>
						<view slot="right" v-if="searchData.ProductCode">
							<u-icon name="close" @click="clearCondition(3)"></u-icon>
						</view>
					</u-form-item>
					<u-form-item label="产品批次" prop="FBatchNo" borderBottom>
						<view @click="openBaseScreen('批次')" style="flex:1;">
							<u--input :value="searchData.FBatchNo" readonly placeholder="请选择批次"
								border="none"></u--input>
						</view>
						<view slot="right" v-if="searchData.FBatchNo">
							<u-icon name="close" @click="clearCondition(4)"></u-icon>
						</view>
					</u-form-item>
				</u--form>

				<view class="searchPanel">
					<u-button type="info" color="#51d0d0" :plain="true" text="重置" @click="reset"></u-button>
					<u-button type="primary" color="#51d0d0" text="确认" @click="searchList"></u-button>
				</view>
			</view>
		</u-popup>

		<!-- PDA扫码 -->
		<m-pda-scancode v-if="$utils.isPda()" listenName="BatchEditScan"
			:SetContent="{action:$store.state.systemConfig.sysDeviceInfo.broadcastAction,extra:$store.state.systemConfig.sysDeviceInfo.broadcastResult}"></m-pda-scancode>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				titleName: '',
				showSearch: false,
				searchData: {
					FWarehouseID: 0,
					FWarehouseName: '',
					FStorageID: 0,
					FStorageCode: '默认库位',
					ProductID: 0,
					ProductCode: '',
					ProductName: '',
					FBatchNo: ''
				},
				totalQty: '',
				dataList: []
			}
		},

		created() {
			let that = this;
			let menu = that.$store.state.Menu;
			that.menu = menu;
			that.titleName = that.menu.APFunName;
		},

		mounted() {
			let that = this;

			// 监听下拉回调
			uni.$on('BatchEditChoice', function(ret) {
				let selectItem = ret.data;

				if (ret.type == '仓库') {
					if (selectItem.FWarehouseID != that.searchData.FWarehouseID) {
						that.searchData.FWarehouseID = selectItem.FWarehouseID;
						that.searchData.FWarehouseName = selectItem.FWarehouseName;

						// 仓库变更-库位清空
						that.searchData.FStorageID = 0;
						that.searchData.FStorageCode = '默认库位';
					}
				}

				if (ret.type == '库位') {
					that.searchData.FStorageID = selectItem.FStorageID;
					that.searchData.FStorageCode = selectItem.FStorageCode;
				}

				if (ret.type == '产品') {
					that.searchData.ProductID = selectItem.ProductID;
					that.searchData.ProductCode = selectItem.ProductCode;
					that.searchData.ProductName = selectItem.ProductName;
				}

				if (ret.type == '批次') {
					that.searchData.FBatchNo = selectItem.FBatchNo;
				}
			})

			// PDA扫码处理
			uni.$on('BatchEditScan', function(content) {
				let scanData = that.$utils.wmsFun.scanTransf(content);
				that.scanHandler(scanData);
			})

		},

		methods: {

			// 关闭弹窗
			closeSearch() {
				this.showSearch = false;
			},

			reset() {
				this.searchData = {
					FWarehouseID: 0,
					FWarehouseName: '',
					FStorageID: 0,
					FStorageCode: '默认库位',
					ProductID: 0,
					ProductCode: '',
					ProductName: '',
					FBatchNo: ''
				}
				this.$refs.paging.reload();
				this.showSearch = false;
			},

			searchList() {
				this.$refs.paging.reload();
				this.showSearch = false;
			},

			// 清楚查询条件
			clearCondition(cType) {
				switch (cType) {
					case 1:
						this.searchData.FWarehouseID = 0;
						this.searchData.FWarehouseName = '';
						this.searchData.FStorageID = 0;
						this.searchData.FStorageCode = '默认库位';
						break;
					case 2:
						this.searchData.FStorageID = 0;
						this.searchData.FStorageCode = '默认库位';
						break;
					case 3:
						this.searchData.ProductID = 0;
						this.searchData.ProductCode = '';
						this.searchData.ProductName = '';
						break;
					case 4:
						this.searchData.FBatchNo = ''
						break;
					default:
						break;
				}
			},

			// 现存量查询
			queryList() {
				let that = this;
				that.Request.request('batchCode_currStock', that.searchData, function(data) {
						that.totalQty = data.data.ds.map(item => item.Qty).
						reduce((arr, curr) => arr + curr, 0);
						that.$refs.paging.setLocalPaging(data.data.ds);
					},
					function(err) {
						that.$refs.paging.complete(false);
					}, that.Request.IPAgreement() + that.Request.serverIP() +
					'/Frame/SqlData', 1)
			},

			// 打开摄像头
			openScan() {
				let that = this;
				this.$scanPopup.openScan({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success: (res) => {
						let scanData = that.$utils.wmsFun.scanTransf(res.result);
						that.scanHandler(scanData);
					}
				})
				/* uni.scanCode({
					onlyFromCamera: true,
					scanType: ['qrCode'],
					success: function(res) {
						let scanData = that.$utils.wmsFun.scanTransf(res.result);
						that.scanHandler(scanData);
					}
				}) */
			},

			// 扫码处理
			scanHandler(scanData) {
				let that = this;

				if (that.scanDisabled) {
					that.$utils.msg('数据提交中,请稍后再试!');
					return false;
				}
				if (!scanData) {
					that.$utils.msg('请扫描正确的条码!');
					return false;
				}

				if (scanData.type == '产品批次码') {

					this.searchData.ProductCode = scanData.data.code;
					this.searchData.FBatchNo = scanData.data.batch;
					this.$refs.paging.reload();

				} else if (scanData.type == '库位') {

					let obj = {
						QrQZ: scanData.data.tag,
						QrCode: scanData.data.code,
						FWarehouseID: that.searchData.FWarehouseID
					}

					that.scanDisabled = true;
					that.Request.request('Get_QrStorage', obj, function(res) {
							that.scanDisabled = false;

							let data = res.data;
							if (data.ds[0].status == 500) {
								that.$utils.msg(data.ds[0].msg)
							} else {
								that.searchData.FStorageID = data.ds[0].FStorageID;
								that.searchData.FStorageCode = data.ds[0].FStorageCode;

								that.searchData.FWarehouseID = data.ds[0].FWarehouseID;
								that.searchData.FWarehouseName = data.ds[0].FWarehouseName;
							}
						}, function(err) {
							that.scanDisabled = false;
						}, that.Request.IPAgreement() + that.Request.serverIP() +
						'/Frame/SqlData', 1)

				}
			},

			// 公共下拉选择
			openBaseScreen(str) {
				let url = '/commonPage/Tool/baseScreen';
				let obj;

				if (str == '仓库') {
					obj = {
						allowClear: false,
						cmd: 'Droplist_Warehouse',
						type: str,
						ListeningName: 'BatchEditChoice',
						refresherEnabled: false,
						serverSearch: {
							isPaging: false, //是否分页
							showText: 'FWarehouseName', //查询结果显示的字段
							ChoiceDataKey: 'FWarehouseID',
							ChoiceDataData: this.searchData.FWarehouseID
						},
						param: {

						}
					}
				}

				if (str == '库位') {
					if (!this.searchData.FWarehouseID) {
						this.$utils.msg('请先选择仓库!');
						return false;
					}

					obj = {
						allowClear: false,
						cmd: 'Droplist_Storage',
						type: str,
						ListeningName: 'BatchEditChoice',
						refresherEnabled: false,
						serverSearch: {
							isPaging: false, //是否分页
							searchKey: 'FStorageCode',
							showText: 'FStorageCode', //查询结果显示的字段
							ChoiceDataKey: 'FStorageID',
							ChoiceDataData: this.searchData.FStorageID
						},
						param: {
							FWarehouseID: this.searchData.FWarehouseID
						}
					}
				}

				if (str == '产品') {
					obj = {
						allowClear: false,
						cmd: 'Droplist_Product',
						type: str,
						ListeningName: 'BatchEditChoice',
						refresherEnabled: false,
						serverSearch: {
							isPaging: false, //是否分页
							searchKey: 'ProductName',
							showText: 'ProductName', //查询结果显示的字段
							ChoiceDataKey: 'ProductCode',
							ChoiceDataData: this.searchData.ProductCode
						},
						param: {
							ProductName: ''
						}
					}
				}

				if (str == '批次') {
					if (!this.searchData.ProductCode) {
						this.$utils.msg('请先选择产品!');
						return false;
					}

					obj = {
						allowClear: false,
						cmd: 'batchCode_batchDrop',
						type: str,
						ListeningName: 'BatchEditChoice',
						refresherEnabled: false,
						serverSearch: {
							isPaging: false, //是否分页
							searchKey: 'FBatchNo',
							showText: 'FBatchNo', //查询结果显示的字段
							ChoiceDataKey: 'ProductCode',
							ChoiceDataData: this.searchData.ProductCode
						},
						param: {
							FWarehouseID: this.searchData.FWarehouseID,
							FStorageID: this.searchData.FStorageID,
							ProductCode: this.searchData.ProductCode
						}
					}
				}

				this.$store.commit('ChangeAndSetStateData', {
					Name1: 'BaseScreenParm',
					data: obj
				})

				uni.navigateTo({
					url: url
				})
			}
		},

		onLoad(option) {

		},

		onUnload() {
			uni.$off('BatchEditChoice');
			uni.$off('BatchEditScan');
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/static/assets/scss/wms.scss";

	.scanButton {
		position: fixed;
		bottom: calc(60px + env(safe-area-inset-bottom));
		right: 20px;
	}

	.itemContent {
		display: flex;
		padding: 2px;
		align-items: center;
		font-size: 14px;

		&_label {
			width: 72px;
			color: #A0A0A0;
		}

		&_value {
			color: #333333;
			margin-left: 3px;
			flex: 1;
		}
	}

	.total {
		display: flex;
		justify-content: flex-end;
		padding: 10px 25px 0px 0px;

		&_label {
			color: #A0E4E4;
		}

		&_value {
			margin-left: 5px;
			color: #51D0D0;
		}
	}
</style>